﻿using System.Collections.ObjectModel;
using System.Windows;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System.Windows.Input;
using ImTools;
using Prism.Commands;
using SoonCode.Core;
using SoonCode.FocusTest.Api;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Utils;
using MessageBox = HandyControl.Controls.MessageBox;

namespace SoonCode.FocusTest.ViewModels
{
    class MemberEditViewModel : BindableBase, IDialogAware
    {
        private string id;
        private IEventAggregator aggregator;
        private ObservableCollection<DictModel> channelList;
        private ObservableCollection<DictModel> statusList;
        private ObservableCollection<ClassesInfo> classesList;
        private int channelSelect = 0;
        private int statusSelect = 0;
        private int sexSelect = 0;
        private int classesSelect = 0;


        public Visibility ClassVisibility { get; set; } =
            GlobalModel.ShowClass() ? Visibility.Visible : Visibility.Collapsed;


        public ObservableCollection<ClassesInfo> ClassesList
        {
            get { return classesList; }
            set
            {
                classesList = value;
                RaisePropertyChanged();
            }
        }

        public int ClassesSelect
        {
            get { return classesSelect; }
            set
            {
                classesSelect = value;
                RaisePropertyChanged();
            }
        }

        public int SexSelect
        {
            get { return sexSelect; }
            set
            {
                sexSelect = value;
                RaisePropertyChanged();
            }
        }

        public int StatusSelect
        {
            get { return statusSelect; }
            set
            {
                statusSelect = value;
                RaisePropertyChanged();
            }
        }

        public int ChannelSelect
        {
            get { return channelSelect; }
            set
            {
                channelSelect = value;
                RaisePropertyChanged();
            }
        }

        public ObservableCollection<DictModel> StatusList
        {
            get { return statusList; }
            set
            {
                statusList = value;
                RaisePropertyChanged();
            }
        }

        public ObservableCollection<DictModel> ChannelList
        {
            get { return channelList; }
            set
            {
                channelList = value;
                RaisePropertyChanged();
            }
        }

        public ObservableCollection<DictModel> SexList
        {
            get
            {
                return new ObservableCollection<DictModel>()
                {
                    new DictModel() { id = "男", name = "男" },
                    new DictModel() { id = "女", name = "女" },
                };
            }
        }

        private string channel;

        public string Channel
        {
            get { return channel; }
            set
            {
                channel=value;
                RaisePropertyChanged();
            }
        }

        private string sex;
        public string Sex
        {
            get { return sex;}
            set
            {
                sex=value;
                RaisePropertyChanged();
            }
        }
        public string Sales { get; set; }
        public string Name { get; set; }
        public DateTime BirthDay { get; set; } = DateTime.Now;
        public string ParentName { get; set; }
        public string Phone { get; set; }
        private string status;
        public string Status
        {
            get { return status;}
            set
            {
                status=value;
                RaisePropertyChanged();
            }
        }

        private string classes = "";

        public string Classes
        {
            get
            {
                return classes;
            }
            set
            {
                classes=value;
                RaisePropertyChanged();
            }
        }

        public string Notes { get; set; }
        public ICommand SaveDataCommand { get; set; }
        public ICommand CloseDialogCommand { get; set; }

        public MemberEditViewModel(IEventAggregator aggregator)
        {
            CloseDialogCommand = new DelegateCommand(() =>
            {
                RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
            });
            SaveDataCommand = new DelegateCommand(async () =>
            {
                if (this.Name.IsNullOrEmpty() || this.BirthDay.IsNullOrEmpty())
                {
                    MessageBox.Show("姓名与生日为必填项，请完善数据后再试", "数据错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                MemberInfo info = new MemberInfo();
                info.id = this.id;
                info.channelId = Channel;
                info.salesPerson = Sales;
                info.name = Name;
                info.sex = Sex;
                info.birth = BirthDay.ToString("yyyy-MM-dd");
                info.parent_name = ParentName;
                info.phone = Phone;
                info.statusId = Status;
                info.notes = Notes;
                info.classId = Classes;
                bool result = await API.SaveMember(info);
                if (result)
                {
                    RequestClose.Invoke(new DialogResult(ButtonResult.OK));
                }
            });
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        private async void getChannels()
        {
            ChannelList = await API.GetChannels();
            this.ChannelSelect = ChannelList.Select(m => m.id).ToArray().IndexOf(this.Channel);
        }

        private async void getStatuses()
        {
            StatusList = await API.GetStatuses();
            this.StatusSelect = StatusList.Select(m => m.id).ToArray().IndexOf(this.Status);
        }


        public void OnDialogOpened(IDialogParameters parameters)
        {
            Task.Run(getChannels);
            Task.Run(getStatuses);
            this.ClassesList = GlobalModel.Instance.ClassesList.DeepClone();
            this.ClassesList.Insert(0, new ClassesInfo());
            MemberInfo info = parameters.GetValue<MemberInfo>("member");
            if (info == null) return;
            this.id = info.id;
            this.Channel = info.channelId;
            this.Sales = info.salesPerson;
            this.Name = info.name;
            this.Sex = info.sex;
            this.BirthDay = info.birth.ToDateTime();
            this.ParentName = info.parent_name;
            this.Phone = info.phone;
            this.Status = info.statusId;
            this.Notes = info.notes;
            this.Classes = info.classId;
            this.SexSelect = SexList.Select(m => m.id).ToArray().IndexOf(this.Sex);
            this.ClassesSelect=ClassesList.Select(m => m.id).ToArray().IndexOf(this.Classes);
        }

        public string Title { get; } = "编辑会员信息";
        public event Action<IDialogResult>? RequestClose;
    }
}